Skip to content

Method: buildAxiomValuesFromInstance(Object, AxiomValueGatherer)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import cz.cvut.kbss.jopa.exceptions.CardinalityConstraintViolatedException;
18: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
19: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
20: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
21: import cz.cvut.kbss.jopa.oom.exceptions.EntityDeconstructionException;
22: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
23: import cz.cvut.kbss.ontodriver.model.Assertion;
24: import cz.cvut.kbss.ontodriver.model.Axiom;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import cz.cvut.kbss.ontodriver.model.Value;
27:
28: import java.net.URI;
29:
30: class SingularObjectPropertyStrategy<X> extends FieldStrategy<Attribute<? super X, ?>, X> {
31:
32: private Object value;
33:
34: SingularObjectPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att,
35: Descriptor descriptor, EntityMappingHelper mapper) {
36: super(et, att, descriptor, mapper);
37: }
38:
39: @Override
40: void addValueFromAxiom(Axiom<?> ax) {
41: assert ax.getValue().getValue() instanceof NamedResource;
42: final NamedResource valueIdentifier = (NamedResource) ax.getValue().getValue();
43: final Object newValue;
44: if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType())) {
45: newValue = IdentifierTransformer
46: .transformToIdentifier(valueIdentifier.getIdentifier(), attribute.getJavaType());
47: } else {
48: newValue = mapper
49: .getEntityFromCacheOrOntology(attribute.getJavaType(), valueIdentifier.getIdentifier(),
50: attributeDescriptor);
51: }
52: verifyCardinality(ax.getSubject());
53: this.value = newValue;
54: }
55:
56: private void verifyCardinality(NamedResource subject) {
57: if (value != null) {
58: throw new CardinalityConstraintViolatedException(
59: "Expected single value of attribute " + attribute.getName() + " of instance " + subject +
60: ", but got multiple.");
61: }
62: }
63:
64: @Override
65: void buildInstanceFieldValue(Object instance) throws IllegalAccessException {
66: setValueOnInstance(instance, value);
67: }
68:
69: @Override
70: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
71: final Object extractedValue = extractFieldValueFromInstance(instance);
72:• Value<?> val = extractedValue != null ? extractReferenceIdentifier(extractedValue) : Value.nullValue();
73: valueBuilder.addValue(createAssertion(), val, getAttributeContext());
74: }
75:
76: private <V> Value<NamedResource> extractReferenceIdentifier(final V value) {
77: if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType())) {
78: return new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(value)));
79: }
80: final EntityType<V> valEt = (EntityType<V>) mapper.getEntityType(value.getClass());
81: if (valEt == null) {
82: throw new EntityDeconstructionException("Value of field " + attribute.getJavaField()
83: + " is not a recognized entity.");
84: }
85: final URI id = resolveValueIdentifier(value, valEt);
86: cascadeResolver.resolveFieldCascading(attribute, value, getAttributeContext());
87: return new Value<>(NamedResource.create(id));
88: }
89:
90: @Override
91: Assertion createAssertion() {
92: return Assertion.createObjectPropertyAssertion(attribute.getIRI().toURI(), attribute.isInferred());
93: }
94: }